package com.zhou.eb.web;

import com.zhou.eb.model.EbProduct;
import com.zhou.eb.model.EbProductCategory;
import com.zhou.eb.service.EbProductCategorySercie;
import com.zhou.eb.service.EbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class EbProductController {

    @Autowired
    private EbProductService ebProductService;

    @Autowired
    private EbProductCategorySercie ebProductCategorySercie;

    @RequestMapping("/detail")
    public String detail(Model model, Integer epId){
        System.out.println("+++++++++++++++++++浏览器点击某个商品+++++++++++++++++++++++++++"+epId);

        //1.调用service查询该商品的详细信息
        EbProduct productDetail = ebProductService.detail(epId);
        //1.1.调用service层查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategorySercie.selectBigAndSmallCate();
        //2.把数据绑定到model
        model.addAttribute("detail",productDetail);
        //2.1 将商品分类进行绑定
        model.addAttribute("categoryMap",categoryMap);
        //3.返回视图名称
        return "product-view";//templates/product-view.html
    }
    //product/cate_product_list/123
    @RequestMapping("/cate_product_list/{cateId}")
    public String productList(Model model,
                              @PathVariable("cateId") Integer cateId,
                              @RequestParam(value = "pSize",defaultValue = "1") Integer pSize,
                              @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize){
        System.out.println("=======进入productList 这个方法=====cateId:"+cateId);
        //1.查询所有属于该二级分类的商品
        List<EbProduct> products = ebProductService.selectByCateId(cateId,pSize,pageSize);
        //1.1.调用service层查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategorySercie.selectBigAndSmallCate();
        //2.将所属分类商品数据绑定到model中

        model.addAttribute("products",products);
        //2.1 将商品分类进行绑定
        model.addAttribute("categoryMap",categoryMap);
        //返回视图
        return "product-list";
    }


}
